package org.reliablesource.prism.core.internal;

import java.util.ArrayList;
import java.util.List;

import org.reliablesource.prism.core.IPrismElement;
import org.reliablesource.prism.core.IPrismNode;

/**
 *
 *	Created on: Feb 25, 2006
 *  @author Dennis Park <a href="mailto:dennis.park@gmail.com">dennis.park@gmail.com</a>
 *
 */

class PrismNode extends PrismElement implements IPrismNode {

	private static final long serialVersionUID = 2359084737540639856L;
	final protected List <IPrismElement>childrenList;	
	
	PrismNode() {
		super();
		childrenList = new ArrayList<IPrismElement>();
	}
	
	
	final public IPrismElement [] getChildren() {
		return childrenList.toArray(new IPrismElement[childrenList.size()]);
	}

	public synchronized boolean addChild(IPrismElement element) {
		boolean _added = false;
		if(element != null){
			_added =childrenList.add(element);
			
			if(_added)
				setChanged();
		}
		return _added;
	}

	public synchronized boolean removeChild(IPrismElement element) {
		boolean _removed  = false;
		if(element != null){
			_removed =childrenList.remove(element);
			
			if(_removed)
				setChanged();
		}
		return _removed;
	}

	
	// FIXME throws exceptions
	public IPrismElement getChild(int sequence) {
		return  childrenList.get(sequence);
	}

	// FIXME throws exceptions
	public boolean containsChild(IPrismElement element) {
		return childrenList.contains(element);
	}

	// FIXME throws exceptions
	public int getChildListSize() {
		return childrenList.size();
	}

	public boolean containsChild(int sequence) {
		return childrenList.contains(childrenList.get(sequence));
	}

	public int getChildSequence(IPrismElement element) {		
		return childrenList.indexOf(element);
	}
}
